package com.jinlejia.erp.base;

import android.os.Build;
import android.os.StrictMode;

import androidx.multidex.MultiDexApplication;

import com.baidu.mapapi.SDKInitializer;
import com.github.promeg.pinyinhelper.Pinyin;
import com.github.promeg.pinyinhelper.PinyinMapDict;
import com.jinlejia.bbs.lab.BBSPushMessageProtocol;
import com.jinlejia.erp.BuildConfig;
import com.jinlejia.erp.base.unicorn.JljUnicornImageLoader;
import com.jinlejia.erp.base.unicorn.UnicornUtils;
import com.jinlejia.erp.lab.MainPushMessageProtocol;
import com.jinlejia.erp.lab.UserLifecycleImpl;
import com.lab.Lab;
import com.lab.LabConfig;
import com.liulishuo.filedownloader.FileDownloader;
import com.mob.MobSDK;
import com.nimlib.utils.NimIMUtils;
import com.qiyukf.unicorn.api.Unicorn;
import com.tencent.bugly.Bugly;
import com.zhy.http.okhttp.OkHttpUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class IApplication extends MultiDexApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        closeAndroidPDialog();
        //第三方
        thirdParty();
        initLab();
        initStruts();
        FileDownloader.setupOnApplicationOnCreate(this);
    }

    private void thirdParty() {
        //Bugly
        Bugly.init(this, "7c30049693", BuildConfig.DEBUG);
        //极光推送
        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(this);
        JPushInterface.requestPermission(this);
        registrationID = JPushInterface.getRegistrationID(this);
        //百度地图
        SDKInitializer.initialize(this);
        //分享
        MobSDK.init(this);
        //网易七鱼
        Unicorn.init(this, "8e6f9610199f8fc6a4b1bb73467fadb0", UnicornUtils.options(), new JljUnicornImageLoader(this));
        //网易云信
        NimIMUtils.init(this);
    }

    private void initLab() {
        LabConfig config = new LabConfig.Builder()
                .userLifecycle(UserLifecycleImpl.class)
                .mainPushProtocol(MainPushMessageProtocol.class)
                .bbsPushProtocol(BBSPushMessageProtocol.class)
                .build();
        Lab.initLab(config);

        //自定义拼音字典
        Pinyin.init(Pinyin.newConfig().with(new PinyinMapDict() {
            @Override
            public Map<String, String[]> mapping() {
                HashMap<String, String[]> map = new HashMap<>();
                map.put("褚", new String[]{"CHU"});
                return map;
            }
        }));
    }

    private void initStruts() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        clientBuilder.connectTimeout(60000L, TimeUnit.MILLISECONDS)
                .readTimeout(60000L, TimeUnit.MILLISECONDS);
        HttpLoggingInterceptor log = new HttpLoggingInterceptor();
        log.setLevel(HttpLoggingInterceptor.Level.BODY);
        clientBuilder.addNetworkInterceptor(log);
        clientBuilder.addInterceptor(new NetInterceptor(this))
                .addInterceptor(new TokenInterceptor(this));
        OkHttpUtils.initClient(clientBuilder.build());
    }

    private static String registrationID;

    public static String getRegistrationID() {
        return registrationID;
    }

    public static void setRegistrationID(String registrationID) {
        IApplication.registrationID = registrationID;
    }

    /**
     * android9.0 detected problems with api
     */
    private void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void fix() {
        try {
            Class clazz = Class.forName("java.lang.Daemons$FinalizerWatchdogDaemon");

            Method method = clazz.getSuperclass().getDeclaredMethod("stop");
            method.setAccessible(true);

            Field field = clazz.getDeclaredField("INSTANCE");
            field.setAccessible(true);

            method.invoke(field.get(null));

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}

